/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月31日         cassie
 */

package com.coocaa.launcher.pattern.normal.desktop;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;

import com.coocaa.launcher.framework.sidebar.ItemContainer;
import com.coocaa.launcher.framework.sidebar.ItemContainer.OnLayoutVisibleChangedListener;
import com.coocaa.launcher.sidebar.left.LeftSideContainer;
import com.coocaa.launcher.statusplugins.tips.StatusPluginTips;
import com.coocaa.launcher.wallpaper.WallpaperManager;
import com.coocaa.x.framework.utils.AsyncTask;
import com.skyworth.logsdk.appstore.LogAPIForAppStore;

public class DesktopController implements OnLayoutVisibleChangedListener, WallpaperManager.OnBackgroundChangeListener {
    private Context mContext;
    private ItemContainer mItemContainer = null;
    private DesktopView desktopView;

    public DesktopController(Context context) {
        this.mContext = context;
        this.desktopView = new DesktopView(mContext);
        WallpaperManager.getInstance().addBackgroundChangeListener(this);
    }

    public DesktopView getView() {
        return desktopView;
    }

    public boolean processKeyDown(int keyCode) {
        if (mItemContainer != null && mItemContainer.isLayoutVisible()) {
            if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU) {
                mItemContainer.hideLayout();
                return true;
            }
        } else {
            if (desktopView.processKeyDown(keyCode))
                return true;
            if (keyCode == KeyEvent.KEYCODE_MENU) {
                desktopView.saveFocusBeforeHide();
                if (mItemContainer == null) {
                    mItemContainer = new LeftSideContainer(mContext);
                    mItemContainer.setLayoutVisibleChangedListener(this);
                    desktopView.addView(mItemContainer.getLayout());
                    mItemContainer.getLayout().setVisibility(View.INVISIBLE);
                }
                mItemContainer.showLayout();
                // 数据采集：调出左侧菜单
                LogAPIForAppStore.leftMenuClick();
                return true;
            }
        }
        return false;
    }

    public void resetHotSeatFocus() {
        desktopView.resetHotSeatFocus();
    }

    @Override
    public void visibleStateChanged(final boolean show) {
        AsyncTask.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (show) {
                        desktopView.hideHotSeat();
                        StatusPluginTips.instance.hide();
                    } else {
                        desktopView.showHotSeat();
                        StatusPluginTips.instance.show();
                    }
                } catch (Exception e) {
                }
            }
        });
    }

    public void destory() {
        if (mItemContainer != null) {
            mItemContainer.destory();
        }
        WallpaperManager.getInstance().removeBackgroundChangeListener(this);

    }

    public ItemContainer getmItemContainer() {
        return mItemContainer;
    }

    public void setmItemContainer(ItemContainer mItemContainer) {
        this.mItemContainer = mItemContainer;
    }

    @Override
    public void onBackgroundChange() {
        if (mItemContainer != null)
            mItemContainer.hideLayout();
    }
}
